
module Blather
class XMPPNode
	class Field < XMPPNode
		register :field, XMPPNode::Form.registered_ns

    VALID_TYPES = [:boolean, :fixed, :hidden, :'text-single'].freeze

		def self.create(opts = {})
			node = self.new
			node.var = opts[:var] if opts[:var]
			node.type = opts[:type] if opts[:type]
			node.label = opts[:label] if opts[:label]
			node
		end

    def type=(type)
      if type && !VALID_TYPES.include?(type.to_sym)
        raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}"
      end
      write_attr :type, type
    end

		def type
			read_attr :type, :to_sym
		end

		def label=(label)
      write_attr :label, label
    end
		def label
			read_attr :label
		end

		def var=(var)
      write_attr :var, var
    end
		def var
			read_attr :var
		end

		def value=(value)
			remove_children ('value')
      set_content_for :value, value
    end

    def value
      content_from 'ns:value', :ns => self.registered_ns
    end
	end
end
end
